<?php

class PythonscrapyFace extends FaceBase
{
	private $weixin = null;
	private $sogou = null;
	private $mongo = null;

	public function __construct()
	{
		parent::__construct();
		$this->weixin = n_api('weixin');
		$this->sogou = n_api('sogou');
		$this->mongo = new MongoClient("mongodb://10.16.43.46:27017");
	}

	public function GetWeixinList()
	{
		$param = $this->param('
			clientIp:char:empty
		');

		$clientIp = $param['clientIp'];

		if ($clientIp == '123.125.123.125') {
			$weixinFinaList = $this->weixin->getWeixinFinaList();
			$weixinDisplay = [];
			foreach ($weixinFinaList as $weixinInfo) {
				$tmp['url'] = $this->sogou->getSogouUrl($weixinInfo['sogouid']);
				$weixinDisplay[] = $tmp;
			}
			echo json_encode($weixinDisplay);
		}
	}

	public function GetWeixinUrlList()
	{
		$sogouCollection = $this->mongo->selectDB('sogou')->selectCollection('weixin_urls');
		$stime = strtotime(date('Y-m-d', strtotime('-2 day')));
		$query = ['ptime' => ['$gte' => $stime]];
		$cursor = $sogouCollection->find($query, ['url'=>1]);
		$data = [];
		foreach ($cursor as $value) {
			$tmp['url'] = $value['url'];
			$tmp['mongoid'] = $value['_id']->{'$id'};
			$data[] = $tmp;
		}
		echo json_encode($data);
	}

	public function GetReadKey()
	{
		$keyInfo = n_api('weixin')->getReadKeyInfo();
		echo json_encode($keyInfo);
	}
}